package com.leadingsoft.pwxk.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.leadingsoft.pwxk.converter.CategoryConverter;
import com.leadingsoft.pwxk.dto.CategoryDto;
import com.leadingsoft.pwxk.excel.ExcelUtil;
import com.leadingsoft.pwxk.model.Category;
import com.leadingsoft.pwxk.repository.CategoryRepository;
import com.leadingsoft.pwxk.service.CategoryService;

@Service
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	CategoryRepository categoryRepository;

	@Autowired
	CategoryConverter categoryConverter;

	@Override
	public void importData(MultipartHttpServletRequest request) throws IOException {
		Iterator<String> filenames = request.getFileNames();
		while (filenames.hasNext()) {
			MultipartFile file = request.getFile(filenames.next());
			// 解析数据为map <行号,数据内容>
			List<Map<Integer, List<String>>> datas = ExcelUtil.parseExcel(file, 1, 1);
			dealData(datas);
		}
	}

	private void dealData(List<Map<Integer, List<String>>> datas) {
		Map<String, Category> map = new HashMap<String, Category>(2048);
		datas.forEach(t1 -> {
			t1.forEach((key, val) -> {
				String key1 = val.get(0);// 门类
				String key2 = val.get(0) + val.get(2);// 大类
				String key3 = val.get(0) + val.get(4);// 中类
				String key4 = val.get(0) + val.get(6); // 小类
				Category category1 = new Category();
				category1.setCode(key1);
				category1.setName(val.get(1));
				Category category2 = new Category();
				category2.setCode(key2);
				category2.setName(val.get(3));
				category2.setParentCode(category1.getCode());
				Category category3 = new Category();
				category3.setCode(key3);
				category3.setName(val.get(5));
				category3.setParentCode(category2.getCode());
				Category category4 = new Category();
				category4.setCode(key4);
				category4.setName(val.get(7));
				category4.setParentCode(category3.getCode());
				map.put(key1, category1);
				map.put(key2, category2);
				map.put(key3, category3);
				map.put(key4, category4);
			});
		});
		map.forEach((key, val) -> {
			categoryRepository.save(val);
		});
	}

	@Override
	public List<CategoryDto> getAll() {
		List<Category> all = categoryRepository.findAll();
		List<Category> root = getRoot(all);
		List<CategoryDto> dtos = new ArrayList<CategoryDto>();
		root.forEach(t -> {
			CategoryDto dto = getChild(categoryConverter.toDto(t), all);
			dtos.add(dto);
		});
		return dtos;
	}

	private List<Category> getRoot(List<Category> all) {
		List<Category> root = new ArrayList<Category>();
		all.forEach(t -> {
			if (null == t.getParentCode()) {
				root.add(t);
			}
		});
		return root;
	}

	private CategoryDto getChild(CategoryDto dto, List<Category> all) {
		List<CategoryDto> childs = new ArrayList<CategoryDto>();
		for (Category t : all) {
			if (null != t.getParentCode() && t.getParentCode().equals(dto.getCode())) {
				CategoryDto temp = categoryConverter.toDto(t);
				childs.add(temp);
				getChild(temp, all);
			}
		}
		if (null != childs && childs.size() > 0) {
			dto.setChilds(childs);
		}
		return dto;
	}

	@Override
	public List<CategoryDto> findByCodeChilds(String code) {
		List<Category> all = categoryRepository.findAll();
		Category root = categoryRepository.findByCode(code);
		List<CategoryDto> dtos = new ArrayList<CategoryDto>();
		CategoryDto dto = getChild(categoryConverter.toDto(root), all);
		dtos.add(dto);
		return dtos;
	}

	@Override
	public List<CategoryDto> getRoot() {
		List<Category> pCategories = categoryRepository.findByParentCodeIsNull();
		List<CategoryDto> list = new ArrayList<CategoryDto>();
		pCategories.forEach(t -> {
			list.add(categoryConverter.toDto(t));
		});
		return list;
	}

	public void getChildCode(Category ca, List<Category> all, List<String> childs) {
		for (Category t : all) {
			if (null != t.getParentCode() && t.getParentCode().equals(ca.getCode())) {
				getChildCode(t, all, childs);
			}
		}
		childs.add(ca.getCode());
	}

	@Override
	public List<CategoryDto> get() {
		List<Category> list = categoryRepository.findAll();
		List<CategoryDto> result = new ArrayList<CategoryDto>(2048);
		list.forEach(t -> {
			result.add(categoryConverter.toDto(t));
		});
		return result;
	}

}
